package com.example.user.controller.basic;

import com.example.framework.common.pojo.R;
import com.example.framework.security.core.util.SecurityFrameworkUtils;
import com.example.user.controller.auth.vo.UserSourceType;
import com.example.user.controller.basic.vo.*;
import com.example.user.dto.AreaRespDto;
import com.example.user.service.map.AreaService;
import com.example.user.service.user.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Tag(name = "用户服务")
@Validated
@RestController
@RequestMapping("/user")
public class UserInfoController {

    @Resource
    private UserService userService;

    @Resource
    private AreaService areaService;

    @PermitAll
    @Operation(summary = "查询用户是否存在")
    @PostMapping("/checkUserExists")
    public R<Boolean> checkUserExists(@Valid @RequestBody UserSourceType reqDto) {
        boolean exists = userService.checkUserExists(reqDto);
        return R.success(exists);
    }

    @Operation(summary = "查询当前登录的用户信息")
    @GetMapping("/getUserInfo")
    public R<UserInfoRespVO> getUserInfo() {
        String userId = SecurityFrameworkUtils.getLoginUserId();
        UserInfoRespVO userInfo = userService.getUserInfo(userId);
        return R.success(userInfo);
    }

    @Operation(summary = "修改用户信息")
    @PostMapping("/info/update")
    public R<Boolean> updateUserInfo(@RequestBody UserInfoUpdateVO userInfoUpdateVO) {
        boolean updated = userService.updateUserInfo(userInfoUpdateVO);
        return R.success(updated);
    }

    @Operation(summary = "修改用户名称")
    @PostMapping("/name/update")
    public R<Boolean> updateUserName(@Valid @RequestBody UserNameUpdateVO reqVO) {
        boolean updated = userService.updateUserName(reqVO);
        return R.success(updated);
    }

    @Operation(summary = "修改用户头像")
    @PostMapping("/avatar/update")
    public R<Boolean> updateUserAvatar(@Valid @RequestBody UserAvatarUpdateVO reqVO) {
        boolean updated = userService.updateUserAvatar(reqVO);
        return R.success(updated);
    }

    @Operation(summary = "修改用户密码")
    @PostMapping("/password/update")
    public R<Boolean> updateUserPassword(@Valid @RequestBody UserPasswordUpdateVO reqVO) {
        boolean updated = userService.updateUserPassword(reqVO);
        return R.success(updated);
    }

    @PermitAll
    @Operation(summary = "获取用户id集合")
    @GetMapping("/getUserIds")
    public R<Set<String>> getUserIds() {
        Set<String> userIds = userService.getUserIds();
        return R.success(userIds);
    }

    @PermitAll
    @Operation(summary = "省市区树")
    @GetMapping("/areaTree/get")
    public R<List<AreaRespDto>> getAreaTree() {
        List<AreaRespDto> result = areaService.getAreaTree();
        return R.success(result);
    }

}